package com.fleafinder;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fleafinder.entity.Category;
import com.fleafinder.entity.Market;

public class GridViewDetailsCategoryItemAdapter extends BaseAdapter {
	private Context context;
	private List<Category> categoryList = null;
	private Market market = null;

	public GridViewDetailsCategoryItemAdapter(Context context, List<Category> categoryList, Market market) {
		this.context = context;
		this.categoryList = categoryList;//DataCenter.getInstance().getCategoryList();
		this.market = market;
	}

	@Override
	public int getCount() {
		return this.categoryList.size() - 1;
	}

	@Override
	public Object getItem(int position) {
		return this.categoryList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return this.categoryList.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService((Context.LAYOUT_INFLATER_SERVICE));

		boolean checked = false;
		for (Category c : market.getCategories()) {
			if (c.getId() == this.categoryList.get(position).getId()) {
				checked = true;
				break;
			}
		}

		View v = null;
		TextView tvCategoryName = null;
		ImageView ivCategoryCheck = null;
		if (convertView == null) {
			v = inflater.inflate(R.layout.market_details_category_item, null);
		} else {
			v = (View) convertView;
		}

		tvCategoryName = (TextView) v.findViewById(R.id.category_name);
		ivCategoryCheck = (ImageView) v.findViewById(R.id.category_check);

		tvCategoryName.setText(this.categoryList.get(position).getName());
		ivCategoryCheck.setImageDrawable(this.context.getResources()
				.getDrawable(checked ? R.drawable.checked : R.drawable.unchecked));

		return v;
	}

	public Market getMarket() {
		return market;
	}

	public void setMarket(Market market) {
		this.market = market;
	}
}
